<!DOCTYPE html>
<html class="x-admin-sm" xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script src="/static/lib/layui/layui.all.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/js/tool.js"></script>
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">演示</a>
                <a>
                    <cite>导航元素</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-input-inline layui-show-xs-block" shiro:hasPermission="orgPermission:add" >
                            <button type="button" class="layui-btn"
                                    onclick="xadmin.open('添加菜单','/orgPermission/add?parent=0&type=1',600,400)"><i
                                    class="layui-icon"></i>添加菜单
                            </button>
                        </div>
                    </form>
                </div>
                <div class="layui-card-body">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th width="70">ID</th>
                            <th>名称</th>
                            <th>路径</th>
                            <th>权限</th>
                            <th>图标</th>
                            <th width="50">排序</th>
                            <th width="50">类型</th>

                            <th width="250">操作</th>
                        </thead>
                        <tbody class="x-cate">
                        <tr th:style="'display:' + @{(${item.parentId} != 0 ? 'none')} + ''"
                            th:attr="cate-id=${item.id},fid=${item.parentId}" th:each="item : ${permission}">
                            <td th:text="${item.id}"></td>
                            <td>
                                <span th:if="${2 == item.type}">&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                <span th:if="${3 == item.type}">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;├</span>

                                <i th:if="${1 == item.type || 2 == item.type}" class="layui-icon x-show" status='true'>&#xe623;</i>
                                <span th:text="${item.name}"></span>
                            </td>
                            <td th:text="${item.url}"></td>
                            <td th:text="${item.permission}"></td>
                            <td>
                                <i class="layui-icon" th:utext="${item.icon}"></i>
                            </td>
                            <td>
                                <input onchange="updateIndex(this)" th:attr="data-id=${item.id}" type="number" min="1"
                                       max="99" class="layui-input x-sort" name="order"
                                       th:value="${item.index}">
                            </td>
                            <td>
                                <button th:if="${1 == item.type}" class="layui-btn layui-btn-sm">菜单</button>
                                <button th:if="${2 == item.type}" class="layui-btn layui-btn-sm layui-btn-normal">页面
                                </button>
                                <button th:if="${3 == item.type}" class="layui-btn layui-btn-sm layui-btn-warm">按钮
                                </button>
                            </td>
                            <td class="td-manage">

                                <button shiro:hasPermission="orgPermission:update" class="layui-btn layui-btn layui-btn-xs"
                                        th:attr="data-type=${item.type},data-id=${item.id}"
                                        onclick="edit(this)"><i class="layui-icon">&#xe642;</i>编辑
                                </button>

                                <button shiro:hasPermission="orgPermission:del" class="layui-btn-danger layui-btn layui-btn-xs" th:attr="data-id=${item.id}"
                                        onclick="deleteId(this)"><i class="layui-icon">&#xe640;</i>删除
                                </button>

                                <button shiro:hasPermission="orgPermission:item" th:if="${1 == item.type || 2 == item.type}"
                                        th:attr="data-type=${item.type},data-parent=${item.id}"
                                        class="layui-btn layui-btn-warm layui-btn-xs"
                                        onclick="addChildren(this)"><i class="layui-icon">&#xe642;</i>添加子栏目
                                </button>

                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['form', 'jquery'], function () {
        let form = layui.form, $ = layui.jquery;

        $(function () {
            $("tbody.x-cate tr[fid!='0']").hide();
            // 栏目多级显示效果
            $('.x-show').click(function () {
                if ($(this).attr('status') == 'true') {
                    $(this).html('&#xe625;');
                    $(this).attr('status', 'false');
                    cateId = $(this).parents('tr').attr('cate-id');
                    $("tbody tr[fid=" + cateId + "]").show();
                } else {
                    cateIds = [];
                    $(this).html('&#xe623;');
                    $(this).attr('status', 'true');
                    cateId = $(this).parents('tr').attr('cate-id');
                    getCateId(cateId);
                    for (var i in cateIds) {
                        $("tbody tr[cate-id=" + cateIds[i] + "]").hide().find('.x-show').html('&#xe623;').attr('status', 'true');
                    }
                }
            })
        })
    });
    var cateIds = [];

    function getCateId(cateId) {
        $("tbody tr[fid=" + cateId + "]").each(function (index, el) {
            id = $(el).attr('cate-id');
            cateIds.push(id);
            getCateId(id);
        });
    }

    function addChildren(e) {

        let parent = $(e).attr("data-parent");
        let type = $(e).attr("data-type");
        type = parseInt(type) + 1;
        xadmin.open('添加子元素', '/orgPermission/add?parent=' + parent + '&type=' + type, 600, 500)
    }

    function edit(e) {
        let id = $(e).attr("data-id");
        let type = $(e).attr("data-type");
        xadmin.open('编辑权限', '/orgPermission/edit?id=' + id + '&type=' + type, 600, 500)
    }

    function updateIndex(e) {
        let val = $(e).val();
        let id = $(e).attr("data-id");

        basecrud.PUTRequest({
            url: '/api/orgPermission/index/' + id,
            data: val,
            success: function (res) {
                baseUtil.msgOkDoing('修改成功')
            }
        })
    }

    function deleteId(e) {
        let id = $(e).attr("data-id");
        layer.confirm('确认删除么', {icon: 3, title: '提示'}, function (index) {
            basecrud.DELRequest({
                url: '/api/orgPermission/' + id,
                success: function (res) {
                    layer.close(index);
                    baseUtil.msgOkDoing(res.msg, function () {
                        xadmin.reload()
                    })
                }
            })
        });
    }

</script>
</body>
</html>
